package com.bh.ly.oa.adapter;

import java.io.IOException;

import com.bh.ly.oa.R;
import com.bh.ly.oa.adapter.MenuAdapter.MenuItem;
import com.bh.ly.oa.util.Util;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSString;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainAdapter extends BaseAdapter {

	NSArray array = null;
	Context context = null;
	
	
	public MainAdapter(Context contex,NSArray array) {
		this.array = array;
		this.context = contex;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return array.count();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return array.objectAtIndex(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		MainItem item = null;
		
		if(convertView==null)
		{
			item = new MainItem();
			convertView = LayoutInflater.from(context).inflate(R.layout.main_item, null);
			
			item.icon = (ImageView) convertView.findViewById(R.id.icon);
			item.name = (TextView) convertView.findViewById(R.id.name);
			
			convertView.setTag(item);
			
		}else 
		{
			item = (MainItem) convertView.getTag();
		}
		
		NSDictionary dictionary = (NSDictionary) array.objectAtIndex(position);
		String name = ((NSString)dictionary.objectForKey("submenuname")).getContent();
		String icon = ((NSString)dictionary.objectForKey("submenuimage")).getContent();
		
		String urlString = ((NSString)dictionary.objectForKey("submenuurl")).getContent();
		item.name.setText(name);
		
		int resId = Util.getDrawableByName(icon);
		item.icon.setImageResource(resId);
//		try {
//			item.icon.setImageBitmap(BitmapFactory.decodeStream(context.getAssets().open(icon)));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		return convertView;
	}
	
	class MainItem
	{
		ImageView icon;
		TextView name;
		NSString url;
	}

}
